package icu.ac.kr.phonebook.activity;

import java.util.ArrayList;

import icu.ac.kr.phonebook.*;
import icu.ac.kr.phonebook.provider.ConstantProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class EditDayDiary extends Activity {

	static final String TAG = "EditDayDiary";
	ArrayAdapter<CharSequence> adspin, adModeSpin;
 	
	String selectedWorkType;
	int selectedMode;
	EditText comment;

	String year;
	String month;
	String date;
	String time;	
	int mode;
	
	static final int PRIVATE = 0;
	static final int PUBLIC = 1;
	static final int COUPLE = 2;
	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.editday);
	
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		year = bundle.getString("year");
		month = bundle.getString("month");
		date = bundle.getString("date");
		time = bundle.getString("time");
		mode = bundle.getInt("mode");
	
		ArrayList<String> modeArray = new ArrayList<String>();
		
		TextView dateTitle = (TextView) findViewById(R.id.DateTitle);
		dateTitle.setText(date + " " +ConstantProvider.getMonth(Integer.parseInt(month))+ " "+ year);
		TextView timeTitle = (TextView) findViewById(R.id.timeTitle);
		
		String timeInterpret = "";
		if (Integer.parseInt(time) < 13)
		{
			if (Integer.parseInt(time)+1 == 13 )
				timeInterpret = time+":01 AM ~ "+ "1:00 PM";
			else
				timeInterpret = time+":01 AM ~ "+(Integer.parseInt(time)+1)+ ":00 AM";
		}
		else
		{
			if (Integer.parseInt(time)+1 == 25)
			timeInterpret = (Integer.parseInt(time) - 12)+":01 PM ~ "+ "1:00 AM";
		
		}
		Log.d(TAG, " mode : "+mode);
		timeTitle.setText(timeInterpret);
		
		comment = (EditText) findViewById(R.id.commentEdit);
		Spinner workSpin = (Spinner) findViewById(R.id.worklistSpin);
		
		adspin = ArrayAdapter.createFromResource(this,R.array.worklist, android.R.layout.simple_spinner_item);
		workSpin.setAdapter(adspin);
		workSpin.setOnItemSelectedListener(spinListener);
				
		switch(mode)
		{
		case PRIVATE:
			modeArray.add("Private");
			break;
		case PUBLIC:
			modeArray.add("Public");
			break;
		case COUPLE:
			modeArray.add("COUPLE");
			break;
		default:
			modeArray.add("Private");
			modeArray.add("Public");
			modeArray.add("Couple");
			break;
		}
		
	
		Spinner modeSpin = (Spinner) findViewById(R.id.securitymodeSpin);
		adModeSpin = new ArrayAdapter(this,android.R.layout.simple_spinner_item, modeArray);
		modeSpin.setAdapter(adModeSpin);
		modeSpin.setOnItemSelectedListener(spinListener);
		
		Button save = (Button) findViewById(R.id.save);
		Button cancel = (Button) findViewById(R.id.cancel);	
		Button back = (Button) findViewById(R.id.back);
		
		save.setOnClickListener(mListener);
		cancel.setOnClickListener(mListener);
		back.setOnClickListener(mListener);
	}

	
	OnItemSelectedListener spinListener = new OnItemSelectedListener(){

		public void onItemSelected(AdapterView<?> view, View v,
				int position, long arg3) {
			// TODO Auto-generated method stub
		
		if(view.getId() == R.id.securitymodeSpin)
			{
			
			switch(mode)
			{
			case PRIVATE:
				selectedMode = PRIVATE;
				break;
			case PUBLIC:
				selectedMode = PUBLIC;
				break;
			case COUPLE:
				selectedMode = COUPLE;
				break;
			default:
				if (position == 0)
					selectedMode = PRIVATE;
				else if (position == 1)
					selectedMode = PUBLIC;
				else if (position == 2)
					selectedMode = COUPLE;

				break;
			}	
					
		}
			if(view.getId() == R.id.worklistSpin)
			{
				selectedWorkType = (String) adspin.getItem(position);
			}
		
	}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};

	
	Button.OnClickListener mListener = new Button.OnClickListener(){

		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			Intent intent = null;
			Bundle bundle = null;
			
			switch(v.getId())
			{
			case R.id.save:
				// save to DB via CP
				Log.d(TAG, "DB will be stored.");

				intent = new Intent();
				bundle = new Bundle();
				bundle.putString("hour", time);
				bundle.putString("workType", selectedWorkType);
				bundle.putString("comment", comment.getText().toString());
				bundle.putInt("mode", selectedMode);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				
				finish();
				break;
			case R.id.cancel:
				// save to DB via CP
				
				intent = new Intent();
				bundle = new Bundle();
				bundle.putString("hour", time);
				bundle.putString("workType", "cancel");
				bundle.putString("comment", "cancel");
				bundle.putInt("mode", selectedMode);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				
				finish();
				break;

			case R.id.back:
				finish();
				break;
	
				
			}
			
		}
		
	};
	
	
}
